<?php
class ImportController extends Vnf_Controller_Action {
    protected $_dir;
	function init() {
        parent::init();
        $this->view->translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');
    }
    
    public function getcatfromproductAction() {
    
        set_time_limit(0); // set has no limit
        
        $objProduct = new Product();
        
        $allProduts = $objProduct->getAllCat();
        
        echo count($allProduts);
        $objCategory = new Category();
        
        $i = 1;
        $countCat = 0;
        $countSub = 0;
        foreach($allProduts as $product):
            echo '<br>' . $i++ . ') '. $strCat = $product->Category;
            //continue;
            $cats = explode(':', $strCat);
            @$cat = $cats[0];
            if ($cat) {
                $db_cat = $objCategory->fetchRow('admin_title ="' . $cat . '"');
                if (count($db_cat) > 0) {
                    $cat_id = $db_cat->category_id;
                    $countCat++;
                } else {
                    $data = array(
                        'admin_title' => $cat,
                        'CatFomProduct' => $cat,
                        'parent' => 1,
                        'status' => 1
                    );
                    $cat_id = $objCategory->insert($data);
                }
                
                // sub cat
                @$subCat = $cats[1];
                if ($subCat) {
                    $db_sub_cat = $objCategory->fetchRow('CatFomProduct ="' . $strCat . '"');
                    if (count($db_sub_cat) > 0) {
                        $countSub++;
                        //$cat_id = $db_cat->category_id;
                    } else {
                        $data = array(
                            'admin_title' => $subCat,
                            'CatFomProduct' => $strCat,
                            'parent' => $cat_id,
                            'status' => 1
                        );
                        $objCategory->insert($data);
                    }
                }
            }
            
            echo "--------------<br>";
        endforeach;
        echo "<br>countCat " .$countCat;
        echo "<br>countSub " .$countSub;

        
        die;
    }
}